import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { LearnBaseInfo, LearnBaseTypeInfo, LearnBaseQuestionInfo } from '@/api/base/types'
import { useLearnBaseQbOneStore } from './learn_base_qb_one'
import { useLearnBaseQbTwoStore } from './learn_base_qb_two'

/**
 *学习题库Store
 */
export const useLearnBaseInfoStore = defineStore(
  'learn_base_info',
  () => {
    /* 试题第一部分Store */
    const learnBaseQbOneStore = useLearnBaseQbOneStore()
    /* 试题第二部分Store */
    const learnBaseQbTwoStore = useLearnBaseQbTwoStore()
    /**
     * 学习题库信息
     */
    const learnBaseInfo = ref<LearnBaseInfo>({})
    /**
     * 学习题库科目
     */
    const learnBaseTypes = computed<LearnBaseTypeInfo[]>(() => learnBaseInfo.value.baseTypes!)

    /**
     * 学习试题
     */

    const questions = computed<any>((): any => (baseType: string) => {
      const questions = [...learnBaseQbOneStore.questions, ...learnBaseQbTwoStore.questions]
      return questions.filter((item: any, index: number) => {
        return item.baseTypeCode === baseType
      })
    })


    /**
     * 题库试题
     */

    const baseQuestions = computed(() => ([...learnBaseQbOneStore.questions, ...learnBaseQbTwoStore.questions]))

    /**
     * 根据考试科目统计试题数量
     */
    const questionCount = computed<any>((): any => (baseType: string) => {
      const learnBaseQbOneCount = learnBaseQbOneStore.questions.filter((item: any, index: number) => {
        return item.baseTypeCode === baseType
      }).length

      const learnBaseQbTwoCount = learnBaseQbTwoStore.questions.filter((item: any, index: number) => {
        return item.baseTypeCode === baseType
      }).length

      return learnBaseQbOneCount + learnBaseQbTwoCount
    })

    /**
     * 精选题库试题数量
     */
    const selectLearnBaseQuestionCount = computed<any>(() => (baseType: string) => {
      if (baseType === 'km1') return learnBaseInfo.value.kmySelectedBase?.questionCount
      else if (baseType === 'km4') {
        return learnBaseInfo.value.kmsSelectedBase?.questionCount
      } else {
        return 0
      }
    })

    /**
     *设置学习题库
     * @param learnBaseInfo
     */
    const setLearnBaseInfo = (data: LearnBaseInfo) => {
      // 试题第一部分Store
      learnBaseQbOneStore.setQuestions(data.questions!.slice(0, Math.floor(data.questions!.length / 2)))
      // 试题第二部分Store
      learnBaseQbTwoStore.setQuestions(data.questions!.slice(Math.floor(data.questions!.length / 2)))
      // 清空试题，试题过度小程序无法持久化，需要分批保存
      data.questions = undefined
      learnBaseInfo.value = data
    }

    /**
     * 学习题库信息
     */
    const clearLearnBaseInfo = () => {
      // 清除虚席题库基本信息
      learnBaseInfo.value = {}
      // 清除第一部分题库
      learnBaseQbOneStore.clearQuestions()
      // 清除第二部分题库
      learnBaseQbTwoStore.clearQuestions()
    }

    return {
      setLearnBaseInfo,
      clearLearnBaseInfo,
      selectLearnBaseQuestionCount,
      learnBaseInfo,
      learnBaseTypes,
      questions,
      questionCount,
      baseQuestions,
    }
  },
  {
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
